package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.SchoolSets;
import com.xjc.business.entity.domain.Sets;
import com.xjc.business.entity.po.sysadmin.SetAddPo;
import com.xjc.business.entity.vo.SetVo;
import com.xjc.business.service.SchoolSetsService;
import com.xjc.business.service.SetsService;
import com.xjc.business.mapper.SetsMapper;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.exception.MyException;
import com.xjc.system.result.Result;
import com.xjc.system.result.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【sets(套餐表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class SetsServiceImpl extends ServiceImpl<SetsMapper, Sets>
    implements SetsService {

    @Autowired
    SchoolSetsService schoolSetsService;

    /**
     * 获取所有套餐信息
     *
     * @return 套餐信息
     */
    @Override
    public Result<List<SetVo>> getSetList() {
        List<Sets> setsList = list();
        List<SetVo> voList = setsList.stream().map(sets -> {
            SetVo setVo = new SetVo();
            setVo.setId(sets.getId());
            setVo.setDuration(sets.getDuration());
            setVo.setSetName(sets.getSetName());
            setVo.setMoney(sets.getMoney());
            setVo.setCreateTime(sets.getCreateTime());
            return setVo;
        }).collect(Collectors.toList());
        return Result.okWithData(voList);
    }

    /**
     * 添加套餐
     *
     * @param setAddPo 套餐信息
     * @return 信息
     */
    @Override
    public Result<String> addSet(SetAddPo setAddPo) {

        LambdaQueryWrapper<Sets> setQueryWrapper = new LambdaQueryWrapper<>();
        setQueryWrapper.eq(Sets::getSetName, setAddPo.getSetName());

        Sets setsFromDB = getOne(setQueryWrapper);

        if(setsFromDB !=null){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.SET_EXIST);
        }

        Sets sets = new Sets();
        sets.setSetName(setAddPo.getSetName());
        sets.setMoney(setAddPo.getMoney());
        sets.setDuration(setAddPo.getDuration());
        sets.setCreateTime(new Date());
        sets.setUpdateTime(new Date());
        save(sets);
        return Result.okWithData("添加成功");
    }

    /**
     * 删除套餐
     * @param id id
     * @return 信息
     */
    @Override
    public Result<String> deleteSet(Integer id) {
        LambdaQueryWrapper<SchoolSets> schoolSetsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolSetsLambdaQueryWrapper.eq(SchoolSets::getSetId,id);
        if(schoolSetsService.count(schoolSetsLambdaQueryWrapper) > 0){
            throw new MyException(ResultCode.SUCCESS.getCode(), MyConstants.SET_USED);
        }
        removeById(id);
        return Result.okWithData("删除成功");
    }
}




